package com.cgbl.cg.vm.home

import android.app.Application
import androidx.databinding.ObservableField
import api.CourseServiceFactory
import api.UserServiceFactory
import api.WorkServiceFactory
import com.robot.base.base.vm.GLBaseViewModel
import com.robot.base.common.api.AbstractViewModelSubscriber
import com.robot.base.configs.AppConstants
import com.robot.base.model.*
import com.robot.base.util.arouter.ARouterUtil
import me.goldze.mvvmhabit.binding.command.BindingAction
import me.goldze.mvvmhabit.binding.command.BindingCommand
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent
import me.goldze.mvvmhabit.http.BaseResponse

class HomeWorksViewModel(application: Application) : GLBaseViewModel(application) {

    /**
     * 热门作品推荐，后台作品列表配置，是热门，由排序小到大进行排序显示数量为4个作品。
     * 默认如果未获取到已上架的作品，或获取少于2个，则整个区域不显示
     */
    val recommendCourseVisit = ObservableField(true)

    val uc = UiChangeEvent()

    class UiChangeEvent {
        val tabData: SingleLiveEvent<List<TypeListBean>> = SingleLiveEvent()

        val hotData: SingleLiveEvent<List<WorksBean>> = SingleLiveEvent()

        val moreData: SingleLiveEvent<List<WorksBean>> = SingleLiveEvent()

        val refreshEvent: SingleLiveEvent<Int> = SingleLiveEvent()

        val ribbonDataEvent = SingleLiveEvent<List<BannerBean>>()
    }

    val toDiscoveryCollectionCommand = BindingCommand<Any>(BindingAction {
        ARouterUtil.goToActivity(AppConstants.Router.COLLECTION.A_DISCOVERY_COLLECTION)
    })

    fun refresh(){
        fetchCourseList()
        getRibbonList()
        page()
    }
    fun fetchCourseList() {
        WorkServiceFactory.homeData()
            .subscribe(object : AbstractViewModelSubscriber<BaseResponse<WorksHomeBean>>(this) {
                override fun onNext(t: BaseResponse<WorksHomeBean>) {
                    recommendCourseVisit.set(
                        !(t.data.hotList.isNullOrEmpty() || (t.data.hotList?.size ?: 0) < 2)
                    )
                    uc.hotData.postValue(t.data.hotList)
                    uc.tabData.postValue(t.data.typeList)
                    uc.refreshEvent.postValue(0)
                }

                override fun onError(e: Throwable) {
                    super.onError(e)
                    uc.refreshEvent.postValue(0)
                }
            })
    }

    fun page(){
        WorkServiceFactory.page(
            mapOf(
                "pageNo" to 1,
                "pageSize" to 4,
                "searchType" to 1,
                "isAccount" to "0"
            )
        ).subscribe(object : AbstractViewModelSubscriber<BaseResponse<PageListBean<WorksBean>>>(this){
            override fun onNext(t: BaseResponse<PageListBean<WorksBean>>) {
                uc.moreData.postValue(t.data.list)
                uc.refreshEvent.postValue(0)
            }

            override fun onError(e: Throwable) {
                super.onError(e)
                uc.refreshEvent.postValue(0)
            }
        })
    }


    fun getRibbonList(){
        UserServiceFactory.getBannerList(
            4
        ).subscribe(object : AbstractViewModelSubscriber<BaseResponse<List<BannerBean>>>(this){
            override fun onNext(t: BaseResponse<List<BannerBean>>) {
                uc.ribbonDataEvent.postValue(t.data)
            }
        })
    }
}